// HookView.h: interface for the CHookView class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HOOKVIEW_H__352262DA_2CFA_48D2_AD29_8E657245EE0F__INCLUDED_)
#define AFX_HOOKVIEW_H__352262DA_2CFA_48D2_AD29_8E657245EE0F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Subclass.h"
#include "ntgraph.h"
class CHookView : public CSubclassWnd  
{
public:
	CHookView();
	virtual ~CHookView();
	virtual LRESULT WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam );

	static CNTGraph m_Graph;
	static _signalType_ m_sigtype;

	static IHost*	m_spHost;
	static tagAddinfo* m_pAddinfo;
	static tagAddinManager* m_pAddinfoManager;


	IProject* m_spProject;
	CComQIPtr<IGPSApplication> m_spApplication;

	long m_baseCommandId;
	long m_addinCommandCount;

	class CSink : 
	public IDispEventImpl<0,CSink,&DIID__IGPSApplicationEvents,&LIBID_GPSOBJECTSLib,1,0>
	{
	public:
		BEGIN_SINK_MAP(CSink)
			SINK_ENTRY_EX(0,DIID__IGPSApplicationEvents,1,OnMessage)
			SINK_ENTRY_EX(0,DIID__IGPSApplicationEvents,2,OnSelectObjectChanged)
			SINK_ENTRY_EX(0,DIID__IGPSApplicationEvents,3,OnMoveSelectObjectChanged)
		END_SINK_MAP()
	
		HRESULT __stdcall OnMessage(IDispatch*cmdMsg)
		{
			ICmdMsg*icmdmsg = (ICmdMsg*)cmdMsg;
			HWND hwnd = NULL;
			LPARAM lparam = NULL;
			WPARAM wparam = NULL;
			UINT	message = WM_NULL;
			icmdmsg->get_TargetHwnd((long*)&hwnd);
			icmdmsg->get_wparam((long*)&wparam);
			icmdmsg->get_lparam((long*)&lparam);
			icmdmsg->get_Message((long*)&message);
			
			switch(message)
			{
			case WM_SETPANLEINFO :
			case WM_SETPROGRESS :
				::SendMessage(hwnd,message,wparam,lparam);
				break;
/*			case WM_GETSIGNAL:
				{
					int prn = HIWORD(wparam);
					m_sigtype = (_signalType_)LOWORD(wparam);
					CComQIPtr<IRinex>spRinex = (IRinex*)lparam;
					if(spRinex == NULL)
						return S_FALSE;
				//	ASSERT(UpDateGraph(prn,m_sigtype,spRinex));

					CString szMsg;
					szMsg.Format("Message GetSingnal,PRN : %d ,signal type : %d",prn,stype);
					AfxMessageBox(szMsg);

				}
				break;*/
			case WM_CLOSE:

				break;
			}
			return S_OK;
		}
		HRESULT __stdcall OnSelectObjectChanged(IDispatch*selObjSet)
		{
			return S_OK;
		}
		HRESULT __stdcall OnMoveSelectObjectChanged(IDispatch*MoveSelObjSet)
		{
			if(MoveSelObjSet==NULL)
				return S_FALSE;
			CComQIPtr<IObjectSet>spObjSet = MoveSelObjSet;
			CComQIPtr<IEnumObject>spEnumObj = spObjSet;
			VARIANT vt;
			vt = spEnumObj->First();
			
			CComQIPtr<IRinex>spRinex = vt.pdispVal;

			if(UpDateGraph(0,m_sigtype,spRinex))
				return S_OK;
			return S_FALSE;
		}
		BOOL UpDateGraph(int prn,_signalType_ sigtype,IRinex *pRinex)
		{
			if(pRinex==NULL)
				return FALSE;
			
			CComQIPtr<IRinex>spRinex = pRinex;
			CComQIPtr<ISignal>spSignal;
			spSignal.CoCreateInstance(CLSID_Signal);
			if(spSignal)
			{
				spSignal->put_DataSource(spRinex);
				spSignal->put_SignalType(sigtype);
				
				CComBSTR title,xLabel,yLabel;
				spSignal->get_Title(&title);
				spSignal->get_xLabel(&xLabel);
				spSignal->get_yLabel(&yLabel);
				
				double dataNumber,minx,miny,maxx,maxy;
				
				dataNumber = spSignal->GetdataNumber();
				minx = spSignal->Getminx();
				miny = spSignal->Getminy();
				maxx = spSignal->Getmaxx();
				maxy = spSignal->Getmaxy();
				
				m_Graph.ClearGraph();
				
				//	m_Graph.SetBackColor (RGB(0,0,0));
				m_Graph.SetAxisColor (RGB(0,255,0));
				m_Graph.SetLabelColor (RGB(128,128,128));
				
				// Control's Frame and Plot area options
				m_Graph.SetPlotAreaColor(RGB(212,222,200));
				
				m_Graph.SetFrameStyle(2); // (1) - Flat
				// (2) - Scope (raised frame and sunken plot area borders)
				// (3) - 3DFrame (a bitmap frame picture)
				
				m_Graph.SetGridColor(RGB(192,192,192));
				m_Graph.SetShowGrid (TRUE);
				
				m_Graph.SetCursorColor (RGB(255,0,0));
				m_Graph.SetTrackMode (1);
				
				m_Graph.SetCaption(_bstr_t(title));
				
				m_Graph.SetPlotAreaColor(RGB(0,0,0));
				m_Graph.SetXLabel(_bstr_t(xLabel));
				m_Graph.SetYLabel(_bstr_t(yLabel));
				
				m_Graph.SetRange(minx,maxx,miny,maxy);
				
				m_Graph.SetElementLineColor(RGB(255,0,0));
				m_Graph.SetElementLinetype(0);
				m_Graph.SetElementWidth(1);
				
				m_Graph.SetElementPointColor(RGB(0,0,255));
				m_Graph.SetElementPointSymbol(3);
				m_Graph.SetElementSolidPoint(TRUE);
				
				m_Graph.SetFormatAxisBottom("%.0f");
				m_Graph.SetFormatAxisLeft("%.3f");
				
				for (long pointIndex = 0; pointIndex < dataNumber; pointIndex++) 
				{
					double data = spSignal->DataFromIndex(pointIndex);
					m_Graph.PlotXY(pointIndex, data, 0);
				}
				spSignal.Release();
				return TRUE;
			}
			return FALSE;
		}

	};
	
	
	class CHostEvents : 
		public IDispEventImpl<1, CHostEvents,&DIID_IHostEvents, &LIBID_ScriptDebuggerHost, 1, 0>
	{
		
	public:

		CHostEvents() {}
/*		BEGIN_COM_MAP(CHostEvents)
		END_COM_MAP()
*/
			
			BEGIN_SINK_MAP(CHostEvents)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x1, ObjectNeed)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x2, ScriptAdd)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x3, ScriptRename)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x4, ScriptRemove)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x5, FormAdd)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x6, FormRename)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x7, FormRemove)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x8, BeforeScriptRun)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0x9, ScriptRun)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0xA, BeforeScriptStop)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0xB, ScriptStop)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0xC, ScriptChange)
				SINK_ENTRY_EX(1, DIID_IHostEvents, 0xD, FormChange)
			END_SINK_MAP()
			
	public:
		
		LPDISPATCH __stdcall ObjectNeed(BSTR, IProject *, IScript *){return NULL;};
		void __stdcall ScriptAdd(IProject *Project, IScript *){
		};
		
		void __stdcall ScriptRename(IProject *, IScript *){};
		void __stdcall ScriptRemove(IProject *, IScript *){};
		void __stdcall FormAdd(IProject * Project, IScript *, IForm *){
		};
		void __stdcall FormRename(IProject *, IScript *, IForm *){};
		void __stdcall FormRemove(IProject *, IScript *, IForm *){};
		void __stdcall BeforeScriptRun(IProject * Project, IScript * Script, VARIANT * AllowRun){};
		void __stdcall ScriptRun(IProject * Project, IScript * Script){};
		void __stdcall BeforeScriptStop(IProject * Project, IScript * Script){};
		void __stdcall ScriptStop(IProject * Project, IScript * Script, long ErrCode){};
		void __stdcall ScriptChange(IProject * Project, IScript * Script){
			Project->SaveToFile(_bstr_t(m_szMacroPath));
		};
		
		void __stdcall FormChange(IProject * Project, IScript * Script, IForm * Form){
			Project->SaveToFile(_bstr_t(m_szMacroPath));
		};
		
	};

	CHostEvents m_HostEvents;
	CSink m_Sink;
	DWORD m_dwCookie;

	static CString m_szMacroPath;
};

#endif // !defined(AFX_HOOKVIEW_H__352262DA_2CFA_48D2_AD29_8E657245EE0F__INCLUDED_)
